<?php 
namespace Common\Service\Pay;
require_once "pay/wxpay/lib/WxPay.Api.php";
require_once 'pay/wxpay/lib/WxPay.Notify.php';
require_once 'pay/wxpay/log.php';   
use Common\ServiceInterface\PayNotifyService;
use Think\Log;
//初始化日志
//$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
//$log = WxLog::Init($logHandler, 15);

class WxNotifyCallbackService extends \WxPayNotify implements PayNotifyService
{
    private $confxig = array(); 
    private $callback = null;
    
    public function __construct($config){     
        $config['REMOTE_ADDR']="121.9.242.204"; 
        $this->config=$config;
    } 
     
    public function notify($callback){
        $this->callback = $callback; 
        $this->Handle(true,$this->config);
    }
    //查询订单
	public function Queryorder($transaction_id)
	{
	     
		$input = new \WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		//var_dump($this->config);
		$result = \WxPayApi::orderQuery($input,$this->config['CONNECT_TIME_OUT'],$this->config);  
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true; 
		}
		return false;
	}    
	
	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{    
		$notfiyOutput = array();     
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确"; 
			return false;
		}
		//查询订单，判断订单真实性 
		$isReal = $this->Queryorder($data["transaction_id"]); 
		if(!$isReal){   
			$msg = "订单查询失败"; 
			return false;
		}else{   
		   //处理支付金额变为元
		   $data['total_fee'] = round($data["total_fee"]/100,2);
		   return call_user_func($this->callback,$data); 
		} 
	}
}
 

